package com.dc.business.datacollectbusiness.service.impl;

import com.dc.business.datacollectbusiness.entity.User;
import com.dc.business.datacollectbusiness.mapper.brandStateMapper;
import com.dc.business.datacollectbusiness.model.brandModel;
import com.dc.business.datacollectbusiness.service.UserService;
import com.dc.business.datacollectbusiness.service.brandStateService;
import com.dc.business.datacollectbusiness.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.info.GitProperties;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class brandStateServiceImpl implements brandStateService {

    @Autowired
    private brandStateMapper brandStateMapper;

    @Autowired
    private UserService userService;
    private GitProperties gitProperties;

    // 查询每个厂家前天、昨天的数据
    @Override
    public Map<String,List<brandModel>> preTwoDayBrandStateList(String brand) {
        String formName = "";
        Map<String, String> brandNameMap = getBrandNameMap();
        for (String key : brandNameMap.keySet()) {
            if (key.equals(brand)) {
                formName = brandNameMap.get(key);
            }
        }
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
        LocalDateTime pre1Yesterday = LocalDateTime.now().minusDays(2);
        LocalDateTime pre2Yesterday = LocalDateTime.now().minusDays(3);
        LocalDateTime pre3Yesterday = LocalDateTime.now().minusDays(4);
        LocalDateTime pre4Yesterday = LocalDateTime.now().minusDays(5);
        LocalDateTime pre5Yesterday = LocalDateTime.now().minusDays(6);
        LocalDateTime pre6Yesterday = LocalDateTime.now().minusDays(7);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 昨天相关
        String yesterdayString = yesterday.format(formatter);
        String startYesterdayString = yesterdayString + " 00:00:00";
        String endYesterdayString = yesterdayString + " 23:59:59";
        // 前天相关
        String pre1YesterdayString = pre1Yesterday.format(formatter);
        String startPre1YesterdayString = pre1YesterdayString + " 00:00:00";
        String endPre1YesterdayString = pre1YesterdayString + " 23:59:59";

        String pre2YesterdayString = pre2Yesterday.format(formatter);
        String startPre2YesterdayString = pre2YesterdayString + " 00:00:00";
        String endPre2YesterdayString = pre2YesterdayString + " 23:59:59";

        String pre3YesterdayString = pre3Yesterday.format(formatter);
        String startPre3YesterdayString = pre3YesterdayString + " 00:00:00";
        String endPre3YesterdayString = pre3YesterdayString + " 23:59:59";

        String pre4YesterdayString = pre4Yesterday.format(formatter);
        String startPre4YesterdayString = pre4YesterdayString + " 00:00:00";
        String endPre4YesterdayString = pre4YesterdayString + " 23:59:59";

        String pre5YesterdayString = pre5Yesterday.format(formatter);
        String startPre5YesterdayString = pre5YesterdayString + " 00:00:00";
        String endPre5YesterdayString = pre5YesterdayString + " 23:59:59";

        String pre6YesterdayString = pre6Yesterday.format(formatter);
        String startPre6YesterdayString = pre6YesterdayString + " 00:00:00";
        String endPre6YesterdayString = pre6YesterdayString + " 23:59:59";


        List<brandModel> yesterdayBrandModels = brandStateMapper.preTwoDayBrandStateList(formName,startYesterdayString,endYesterdayString);
        List<brandModel> pre1YesterdayBrandModels = brandStateMapper.preTwoDayBrandStateList(formName,startPre1YesterdayString, endPre1YesterdayString);
        List<brandModel> pre2YesterdayBrandModels = brandStateMapper.preTwoDayBrandStateList(formName,startPre2YesterdayString, endPre2YesterdayString);
        List<brandModel> pre3YesterdayBrandModels = brandStateMapper.preTwoDayBrandStateList(formName,startPre3YesterdayString, endPre3YesterdayString);
        List<brandModel> pre4YesterdayBrandModels = brandStateMapper.preTwoDayBrandStateList(formName,startPre4YesterdayString, endPre4YesterdayString);
        List<brandModel> pre5YesterdayBrandModels = brandStateMapper.preTwoDayBrandStateList(formName,startPre5YesterdayString, endPre5YesterdayString);
        List<brandModel> pre6YesterdayBrandModels = brandStateMapper.preTwoDayBrandStateList(formName,startPre6YesterdayString, endPre6YesterdayString);

        Map<String, List<brandModel>> result = new HashMap<>();
        result.put("前1天数据",yesterdayBrandModels);
        result.put("前2天数据",pre1YesterdayBrandModels);
        result.put("前3天数据",pre2YesterdayBrandModels);
        result.put("前4天数据",pre3YesterdayBrandModels);
        result.put("前5天数据",pre4YesterdayBrandModels);
        result.put("前6天数据",pre5YesterdayBrandModels);
        result.put("前7天数据",pre6YesterdayBrandModels);

        return result;
    }

    @Override
    public List<Map<String, String>> todayBrandStateList() {
        HashMap<String, Object> map = new HashMap<>();
        map = ThreadLocalUtil.get();
        User user = userService.findUserByUsername(map.get("username").toString());
        System.out.println(user.toString());

        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 昨天相关数据
        String yesterdayString = yesterday.format(formatter);
        String startYesterdayString = yesterdayString + " 00:00:00";
        String endYesterdayString = yesterdayString + " 23:59:59";
        List<Map<String, String>> result = new ArrayList<>();
        Map<String, String> brandNameMap = getBrandNameMap();

        for (String key : brandNameMap.keySet()) {
            List<brandModel> yesterdayBrandModels = brandStateMapper.preTwoDayBrandStateList(brandNameMap.get(key), startYesterdayString, endYesterdayString);
            Map<String, String> part = new HashMap<>();
            part.put("厂商名称",key);

            // 解决暂时缺失数据的厂家
            if(key.equals("天津国能津能滨海热电有限公司") || key.equals("天津大站水泥有限公司") || key.equals("一汽丰田汽车有限公司") || key.equals("天津力神新能源科技有限公司")
                    || key.equals("秦皇岛恩彼碧轴承有限公司") || key.equals("神钢汽车铝材") || key.equals("天津市宇润德金属制品有限公司") || key.equals("中策橡胶(天津)有限公司")) {
                part.put("数据情况", "正常");
            }
            part.put("数据情况", "正常");

            if (key.equals("恩智浦半导体(天津)有限公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂- 二次能源-电力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),400000,500000)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),5000,13000)) {
                            part.put("数据情况", "异常");
                        }
                    }
                }
            }

            if (key.equals("天津力神电池股份有限公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂- 二次能源-电力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),14344,569625)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),320,17393)) {
                            part.put("数据情况", "异常");
                        }
                    }
                }
            }

            if (key.equals("天津仁泰新材料股份有限公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂- 二次能源-电力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),19971,57359.4)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),3700,9197)) {
                            part.put("数据情况", "异常");
                        }
                    }
                }
            }

            if (key.equals("天津市天洋发线材有限公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂- 二次能源-电力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),16950,104000)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),1598.937,9728.802)) {
                            part.put("数据情况", "异常");
                        }
                    }
                }
            }

            if (key.equals("天津中玻北方新材料有限责任公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂-二次能源-电力-购进已消费/日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),560,68600)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂-一次能源-天然气（气态）-购进已消费/日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),146292.3181,206009.444)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if (yesterdayBrandModel.getName().equals("全厂-二次能源-燃料油-购进已消费/日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),2,50)) {
                            part.put("数据情况", "异常");
                        }
                    }
                }
            }

            if (key.equals("天津金鹏铝材制造有限公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂- 二次能源-电力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),3640,146160)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),206,18119)) {
                            part.put("数据情况", "异常");
                        }
                    }
                }
            }

            if (key.equals("中芯国际集成电路制造(天津)有限公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂- 二次能源-电力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),20355.0336,4889364.8768)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),674.128,75886.3296)) {
                            part.put("数据情况", "异常");
                        }
                    }
                }
            }

            if (key.equals("中海石油(中国)有限公司天津分公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂- 二次能源-柴油-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),2.84,2618.65124)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 二次能源-电力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),1136347.98,3743852)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 二次能源-热力-购进已消费-日")) {
                        if(yesterdayBrandModel.getStatDate().getMonthValue() >= 11 || yesterdayBrandModel.getStatDate().getMonthValue() <= 3) {
                            if (!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),7,97)) {
                                part.put("数据情况", "异常");
                            }
                        } else {
                            if (!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),0,0)) {
                                part.put("数据情况", "异常");
                            }
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 能效指标-单位油气产量综合能耗-验证-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),52.307,270.001)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 其它数据-综合能源消费量-验证-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),4647.859567,17753.817005)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-产出-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),786,280562.1814)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),3165289.81,12245822.48)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-原油-产出-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),17776.3158,24216611.3525)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-原油-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),17.3172188,92.77165208)) {
                            part.put("数据情况", "异常");
                        }
                    }

                }
            }

            if (key.equals("中粮佳悦(天津)有限公司")) {
                for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                    if (yesterdayBrandModel.getName().equals("全厂- 二次能源-电力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),78960,381080)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 一次能源-天然气（气态）-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),45,12780)) {
                            part.put("数据情况", "异常");
                        }
                    }
                    if(yesterdayBrandModel.getName().equals("全厂- 二次能源-热力-购进已消费-日")) {
                        if(!isNormal(Double.parseDouble(yesterdayBrandModel.getDataValue()),17.262661,5158.89)) {
                            part.put("数据情况", "异常");
                        }
                    }
                }
            }
            part.put("数据状态", yesterdayString + "数据已上报");
            for (brandModel yesterdayBrandModel : yesterdayBrandModels) {
                if (yesterdayBrandModel.getUploadDate() == null) {
                    part.put("数据状态", yesterdayString + "数据未上报");
                }
            }
            result.add(part);
        }
        return result;
    }


    // 判断数据情况（正常/异常)
    public Boolean isNormal(double dataValue, double low, double high) {
        return dataValue >= low && dataValue <= high;
    }

    // 公司名称和数据表名的键值对映射
    public Map<String, String> getBrandNameMap() {

        Map<String, String> brandNameMap = new HashMap<>();
        brandNameMap.put("天津国能津能滨海热电有限公司","forward_binhairedian");
        brandNameMap.put("天津大站水泥有限公司","forward_DaZhanShuiNi");
        brandNameMap.put("恩智浦半导体(天津)有限公司","forward_EnZhiPu");
        brandNameMap.put("一汽丰田汽车有限公司","forward_FengTian");
        brandNameMap.put("天津金鹏铝材制造有限公司","forward_JinPengLvCai");
        brandNameMap.put("天津力神电池股份有限公司","forward_LiShenXinDianChi");
        brandNameMap.put("天津力神新能源科技有限公司","forward_LiShenXinNengYuan");
        brandNameMap.put("秦皇岛恩彼碧轴承有限公司","forward_QinHuangDaoEnBiB");
        brandNameMap.put("天津仁泰新材料股份有限公司","forward_RenTaiXinCaiLiao");
        brandNameMap.put("神钢汽车铝材","forward_ShenGangLvCai");
        brandNameMap.put("天津市天洋发线材有限公司","forward_TianYangFaXianCai");
        brandNameMap.put("天津市宇润德金属制品有限公司","forward_YvRunDe");
        brandNameMap.put("天津中玻北方新材料有限责任公司","forward_ZhongBoBoLi");
        brandNameMap.put("中策橡胶(天津)有限公司","forward_ZhongCe");
        brandNameMap.put("中海石油(中国)有限公司天津分公司","forward_ZhongHaiYou");
        brandNameMap.put("中粮佳悦(天津)有限公司","forward_ZhongLiangJiaYv");
        brandNameMap.put("中芯国际集成电路制造(天津)有限公司","forward_ZhongXinGuoJi");

        return  brandNameMap;
    }

}
